package 力扣._516最长回文子序列;

public class Solution {
    public int longestPalindromeSubseq(String s) {
        //dp[i][j]: s[i][j]之间的回文子串最大长度
        int[][] dp = new int[s.length()][s.length()];
        //初始化
        for (int i = 0; i < s.length(); i++) {
            dp[i][i] = 1;
        }
        //遍历方向从下向上，从左往右(由数据依赖关系所确定)
        for (int i = s.length() - 1; i >= 0; i--) {
            for (int j = i; j < s.length(); j++) {
                if (s.charAt(i) == s.charAt(j))
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                else
                    dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
            }
        }
        return dp[s.length() - 1][s.length() - 1];
    }
}
